package cn.iocoder.yudao.module.exam.service.questionoption;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;
import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.exam.controller.admin.questionoption.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.questionoption.QuestionOptionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.questionoption.QuestionOptionMapper;
import org.thymeleaf.expression.Lists;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.exam.enums.ErrorCodeConstants.*;

/**
 * 试题选项 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class QuestionOptionServiceImpl implements QuestionOptionService {

    @Resource
    private QuestionOptionMapper questionOptionMapper;

    @Override
    public Long createQuestionOption(QuestionOptionSaveReqVO createReqVO) {
        // 插入
        QuestionOptionDO questionOption = BeanUtils.toBean(createReqVO, QuestionOptionDO.class);
        questionOptionMapper.insert(questionOption);
        // 返回
        return questionOption.getId();
    }

    @Override
    public void updateQuestionOption(QuestionOptionSaveReqVO updateReqVO) {
        // 校验存在
        validateQuestionOptionExists(updateReqVO.getId());
        // 更新
        QuestionOptionDO updateObj = BeanUtils.toBean(updateReqVO, QuestionOptionDO.class);
        questionOptionMapper.updateById(updateObj);
    }

    @Override
    public void deleteQuestionOption(Long id) {
        // 校验存在
        validateQuestionOptionExists(id);
        // 删除
        questionOptionMapper.deleteById(id);
    }

    private void validateQuestionOptionExists(Long id) {
        if (questionOptionMapper.selectById(id) == null) {
            throw exception(QUESTION_OPTION_NOT_EXISTS);
        }
    }

    @Override
    public QuestionOptionDO getQuestionOption(Long id) {
        return questionOptionMapper.selectById(id);
    }

    @Override
    public PageResult<QuestionOptionDO> getQuestionOptionPage(QuestionOptionPageReqVO pageReqVO) {
        return questionOptionMapper.selectPage(pageReqVO);
    }

    @Override
    public void save(Long bankId, Long questionId, List<QuestionOptionSaveReqVO> options) {
        List<Long> nuDeleted = options.stream().filter(o -> o.getId() != null && o.getId() > 0).map(QuestionOptionSaveReqVO::getId).collect(Collectors.toList());
        questionOptionMapper.delete(Wrappers.<QuestionOptionDO>lambdaQuery().eq(QuestionOptionDO::getQuestionId,questionId).notIn(nuDeleted.size()>0,QuestionOptionDO::getId,nuDeleted));
        List<QuestionOptionDO> insertOptions = new ArrayList<>();
        List<QuestionOptionDO> updateOptions = new ArrayList<>();
        for (QuestionOptionSaveReqVO op : options) {
            if(op.getId() != null && op.getId() > 0){
                QuestionOptionDO qdo = new QuestionOptionDO();
                org.springframework.beans.BeanUtils.copyProperties(op, qdo);
                qdo.setCorrected(op.getCorrect()!=null&&op.getCorrect()?1:0);
                if(bankId!=null){
                    qdo.setBankId(bankId);
                }
                updateOptions.add(qdo);
            }else {
                QuestionOptionDO qdo = new QuestionOptionDO();
                org.springframework.beans.BeanUtils.copyProperties(op, qdo);
                if(bankId!=null){
                    qdo.setBankId(bankId);
                }
                qdo.setQuestionId(questionId);
                qdo.setOptionUrl(op.getOptionUrl());
                qdo.setOptionText(op.getOptionText());
                qdo.setCorrected(op.getCorrect()!=null&&op.getCorrect()?1:0);
                insertOptions.add(qdo);
            }
        }

        if(!CollectionUtils.isEmpty(insertOptions)){
            questionOptionMapper.insertBatch(insertOptions);
        }
        if(!CollectionUtils.isEmpty(updateOptions)){
            questionOptionMapper.updateBatch(updateOptions);
        }

    }

    @Override
    public List<QuestionOptionSaveReqVO> findOptions(Long bankId, Long questionId) {
        List<QuestionOptionDO> questionOptionDOS = questionOptionMapper.selectList(Wrappers.<QuestionOptionDO>lambdaQuery().eq(bankId!=null,QuestionOptionDO::getBankId, bankId).eq(QuestionOptionDO::getQuestionId, questionId));
        return BeanUtils.toBean(questionOptionDOS, QuestionOptionSaveReqVO.class);
    }

}